﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;

namespace ZOIL.Framework.Core
{
    /// <summary>
    /// Interop stuff needed for querying the client region of a window
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "RECT")]
    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        private int _left;
        private int _top;
        private int _right;
        private int _bottom;

        /// <summary>
        /// 
        /// </summary>
        public int Left
        {
            get
            {
                return _left;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public int Top
        {
            get
            {
                return _top;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public int Right
        {
            get
            {
                return _right;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public int Bottom
        {
            get
            {
                return _bottom;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="right"></param>
        /// <param name="bottom"></param>
        public RECT(int left, int top, int right, int bottom)
        {
            _left = left;
            _top = top;
            _right = right;
            _bottom = bottom;
        }

        /// <summary>
        /// 
        /// </summary>
        public int Height { get { return Bottom - Top; } }
        
        /// <summary>
        /// 
        /// </summary>
        public int Width { get { return Right - Left; } }
        
        /// <summary>
        /// 
        /// </summary>
        public Size Size { get { return new Size(Width, Height); } }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return _left ^ _right ^ _top ^ _bottom;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (!(obj is RECT))
                return false;
            else
                return Equals((RECT)obj);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public bool Equals(RECT rect)
        {
            return (this._left == rect._left && this._right == rect._right && this._top == rect._top && this._bottom == rect._bottom);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static bool operator ==(RECT rect1, RECT rect2)
        {
            return rect1.Equals(rect2);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static bool operator !=(RECT rect1, RECT rect2)
        {
            return !rect1.Equals(rect2);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public static class NativeMethods
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
    }
}
